$(function () {
    var member_datagrid =   $("#member_datagrid");
    var memberAll_datagrid = $("#memberAll_datagrid");
    var open_dialog = $("#open_dialog");
    var change_form = $("#change_form");
    member_datagrid.datagrid({
        url: '/member/selectByChangeKey.do',
        fit: true,
        fitColumns: true,
        toolbar: '#member_toolbar',
        pagination: true,
        rownumber: true,
        singleSelect: true,
        columns: [[
            {field: 'id', checkbox: true},
            {field: 'cardNumber', title: '会员卡号', width: 80},
            {field: 'name', title: '会员名称', width: 80},
            {field:'birthday',title:'会员生日',width:80},
            {field:'levelName',title:'会员等级',width:80},
            {field: 'type', title: '变动类型', width: 80,formatter: function (value, row, index) {
                    console.log(value);
                    if(row.changePoint) {
                        return row.changePoint.type ? "增加" : "<font color='red'>扣除</font>";
                    }
                }
            },
            {field: 'number', title: '变动数额', width: 80,formatter:function(value,row,index) {
                    if(row.changePoint){
                        return row.changePoint.number;
                    }
                }},
            {field: 'date', title: '变动时间', width: 80,formatter:function (value,row,index) {
                    if(row.changePoint){
                        return row.changePoint.date;
                    }
                }},
            {field: 'mark', title: '备注', width: 80,formatter:function (value,row,index) {
                    if(row.changePoint){
                        return row.changePoint.mark;
                    }
                }}
        ]]
    });
    memberAll_datagrid.datagrid({
        url:'/member/query.do',
        width:1350,
        height:250,
        fitColumns:true,
        toolbar:'#member_toolbar',
        pagination:true,
        rownumber:true,
        singleSelect:true,
        columns:[[

            {field:'id',checkbox:true},
            {field:'cardNumber',title:'会员卡号',width:80},
            {field:'name',title:'会员名称',width:80},
            {field:'tel',title:'电话',width:80},
            {field:'birthday',title:'会员生日',width:80},
            {field:'levelName',title:'会员等级',width:80},
            {field:'groupName',title:'会员分组',width:80},
            {field:'employeeName',title:'开卡员工',width:80},
            {field:'balance',title:'余额',width:80},
            {field:'address',title:'地址',width:80},
            {field:'points',title:'积分',width:80},
            {field:'sourceName',title:'客户来源',width:80}
        ]],
        onDblClickRow:function (index,row) {
            var row = memberAll_datagrid.datagrid('getSelected');
            console.log(row);
            var id = row.id;
            var cardNumber = row.cardNumber;
            var name = row.name;
            var levelName = row.levelName;
            var birthday = row.birthday;
            var balance = row.balance;
            var points = row.points;
            var number = row.number;
            var sourceName = row.sourceName;
            $("#id").val(id);
            $("#cardNumber").val(cardNumber);
            $("#name").val(name);
            $("#levelName").val(levelName);
            $("#birthday").val(birthday);
            $("#balance").val(balance);
            $("#points").val(points);
            $("#number").val(number);
            $("#sourceName").val(sourceName);
            open_dialog.dialog('close');
            $("#memberAll_datagrid").datagrid('reload');
        }
    });
    open_dialog.dialog({
        title: 'My Dialog2',
        width: 700,
        height: 200,
        closed: true,
        cache: false,
        modal: true
    });
    var methodObj = {
        query:function (){
            //获取查询条件
            var keyword =  $("#keyword").textbox('getValue');
            //通过datagrid 的 load方法传递参数
            memberAll_datagrid.datagrid('load',{
                keyword:keyword
            });
            if(!keyword){
            $.messager.alert("温馨提示", "请输入卡号/姓名/电话", "warning");
                    return;
            }
                open_dialog.dialog('open');
        },
         save:function(){
             var number = $("#changeNumber").val();
             var mark = $("#changeMark").val();
             var name = $("#name").val("getValue");
             var levelName = $("#levelName").val("getValue");
             var checked = $("input[name='changeType']").prop('checked');
             if(checked){
                 $("#change_form").form("submit", {
                     url: "/changePoint/addPoint.do",
                     onSubmit: function (param) {
                         param.number = number;
                         param.name = name;
                         param.levelName = levelName;
                         param.mark = mark;
                     },
                     success: function () {
                         //重新加载表单
                         $("#change_form").datagrid("load", {
                             number: number,
                             mark:mark
                         });
                     }
                 });
                 return;
             }
             $("#change_form").form("submit", {
                 url: "/changePoint/removePoint.do",
                 onSubmit: function (param) {
                     param.number = number;
                     param.name = name;
                     param.levelName = levelName;
                     param.mark = mark;
                 },
                 success: function () {
                     //重新加载表单
                     $("#member_datagrid").datagrid("load", {
                         number: number,
                         mark:mark
                     })
                 }
             })
        },
        deletePoint:function () {
            var row = member_datagrid.datagrid('getSelected');
            if(!row){
                $.messager.alert("温馨提示", "至少要选择一条数据", "warning");
                return;//终止操作
            }
            $.messager.confirm('确认', '您确认想要执行操作吗？', function (r) {
                if (r) {
                    //给后台发送后台
                    $.get("/member/zeroPoint.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功', 'info', function () {
                                //加载刷新到当前界面
                                memberAll_datagrid.datagrid('reload');
                            })
                        } else {
                            $.messager.alert("温馨提示", "操作失败", "error");
                        }
                    })
                }
            })
        }
    };
    $("[data-cmd]").click(function () {
        var methodname = $(this).data("cmd");
        //动态获取调用方法名字,调用方法;
        methodObj[methodname]();
    });
});